package com.rongshu.sendmessage.activity;

import android.content.Intent;
import android.content.res.ColorStateList;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.core.content.ContextCompat;

import com.gyf.immersionbar.ImmersionBar;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.rongshu.sendmessage.R;
import com.rongshu.sendmessage.base.BaseActivity;
import com.rongshu.sendmessage.util.ClientPublicParamUtil;
import com.rongshu.sendmessage.util.ProgressDialog;
import com.rongshu.sendmessage.util.StringUtils;
import com.rongshu.sendmessage.util.ToastUtils;
import com.rongshu.sendmessage.util.downTime.MCountDownTimer;
import com.rongshu.sendmessage.util.httpUtil.HYContent;
import com.rongshu.sendmessage.util.httpUtil.IModel;
import com.rongshu.sendmessage.view.TitileLayout;

import org.json.JSONException;

import java.util.HashMap;
import java.util.Map;

import butterknife.ButterKnife;

import static com.rongshu.sendmessage.util.DialogUtil.hideProgress;
import static com.rongshu.sendmessage.util.DialogUtil.showProgress;

public class ForgetPasswordActivity extends BaseActivity {

    EditText password_et;
    EditText account_et;
    EditText password_et_two;
    Button regist_btn;
    EditText code_et;
    TextView getCode_btn;
    private ProgressDialog dialog;
    private MCountDownTimer countDownTimer;
    private String source;
    private String wxJson;
    private LinearLayout ll_wx_nick;
    private TextView tv_wx_nice;
    private ImageView iv_yanzheng_1;
    private boolean isClick=true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_password);
        mContext=this;
        ButterKnife.bind(this);
        source=getIntent().getStringExtra("source");
        initView();
        countDownTimer = new MCountDownTimer();
        this.countDownTimer.setOnCountDownListener(new MCountDownTimer.OnCountDownListener() {
            @Override
            public void onStop() {
                if (getCode_btn != null) {
                    getCode_btn.setText("获取验证码");
                    isClick=true;
                }
            }

            @Override
            public void onTick(long second) {
                if (getCode_btn != null) {
                    getCode_btn.setText("获取验证码");
                    String time = "发送中(%s)";
                    time = String.format(time, second);
                    getCode_btn.setText(time);
                    isClick=false;
                }
            }
        });


    }

    private void initView() {
        ll_wx_nick=findViewById(R.id.ll_wx_nick);
        tv_wx_nice=findViewById(R.id.tv_wx_nice);
        account_et=findViewById(R.id.account_et);
        password_et_two=findViewById(R.id.password_et_two);
        password_et=findViewById(R.id.password_et);
        regist_btn=findViewById(R.id.regist_btn);
        code_et=findViewById(R.id.code_et);
        iv_yanzheng_1=findViewById(R.id.iv_yanzheng_1);
        code_et.setOnFocusChangeListener((v, hasFocus) -> {
            if (!hasFocus&&code_et.getText()!=null&&code_et.getText().length()>=4){
                yzCode();
            }

        });
        getCode_btn=findViewById(R.id.getCode_btn);
        View titleLayLeft = findViewById(R.id.titleLayLeft);
        TextView title = findViewById(R.id.title);
        title.setText("忘记密码");
        titleLayLeft.setOnClickListener(this::onClick);

        if ("weixinLogin".equals(source)){
            wxJson=getIntent().getStringExtra("wxJson");
            ll_wx_nick.setVisibility(View.VISIBLE);
            try {
                org.json.JSONObject jsonObject=new org.json.JSONObject(wxJson);
                String name=jsonObject.getString("name");
                tv_wx_nice.setText(name);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }else{
            ll_wx_nick.setVisibility(View.GONE);
        }
    }

    private Intent mIntent;
    public void onClick(View view) {
        int id = view.getId();//获取验证码
        if (id == R.id.getCode_btn) {
            if(!isClick){
                return;
            }
            String account = account_et.getText().toString();
            if (StringUtils.isEmpty(account)) {
                ToastUtils.show(mContext, "手机号码不能为空！");
                return;
            }
            countDownTimer.cancel();
            countDownTimer.start();
            getVCode(account);
            //注册
        } else if (id == R.id.regist_btn) {
            String account1 = account_et.getText().toString();
            if (StringUtils.isEmpty(account1)) {
                ToastUtils.show(mContext, "手机号码不能为空！");
                return;
            }
            String password = password_et.getText().toString();
            String password_two = password_et_two.getText().toString();
            if (StringUtils.isEmpty(password) || StringUtils.isEmpty(password_two)) {
                ToastUtils.show(mContext, "密码不能为空！");
                return;
            }
            if (!password.equals(password_two)) {
                ToastUtils.show(mContext, "两次密码不一致");
                return;
            }
            if (password.length() < 6) {
                ToastUtils.show(mContext, "密码长度不能少于6位！");
                return;
            }

            String vcode = code_et.getText().toString();
            if (StringUtils.isEmpty(vcode)) {
                ToastUtils.show(mContext, "验证码不能为空！");
                return;
            }
            showProgress(mContext);
            //去验证code
            setPassword(account1, password);
        }else if(view.getId()==R.id.titleLayLeft){
            finish();
        }
    }
    private void yzCode() {
        String code = code_et.getText().toString();
        String account = account_et.getText().toString();
        YanZhengCode(account,code);
    }

    private void YanZhengCode(String account, String vcode) {
        Map<String, String> param = new HashMap<>();
        param.put("userName",account);
        param.put("validateCode",vcode);
        param.put("clientPublicParam", ClientPublicParamUtil.getPublicParams(mContext));
        String url= new HYContent().CODE_YANZHENG_URL;
        OkGo.<String>post(url).params(param).execute(new StringCallback() {
            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onSuccess(Response<String> response) {
                IModel.AjaxResult jr = new IModel.AjaxResult(response.body(),false);
                final int code = jr.getCode();
                if(code > 0) {
                    iv_yanzheng_1.setImageTintList(ColorStateList.valueOf(ContextCompat.getColor(mContext,R.color.main_color_new_1)));
                }else{
                    ToastUtils.show(mContext,jr.getMsg());
                }
            }
            @Override
            public void onError(Response<String> response) {
                ToastUtils.show(mContext,"操作失败");
            }
        });
    }
    //  替换新密码
    private void setPassword(String account, String password) {
        Map<String, String> param = new HashMap<>();
        param.put("userName",account);
        param.put("newPassword",password);
        param.put("clientPublicParam", ClientPublicParamUtil.getPublicParams(mContext));
        String url= new HYContent().FORGET_PASSWORD_URL;
        OkGo.<String>post(url).params(param).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                IModel.AjaxResult jr = new IModel.AjaxResult(response.body(),false);
                final int code = jr.getCode();
                if(code > 0) {
                    hideProgress();
                    ToastUtils.show(mContext,jr.getMsg());
                    finish();
                }else{
                    ToastUtils.show(mContext,jr.getMsg());
                    hideProgress();
                }
            }
            @Override
            public void onError(Response<String> response) {
                ToastUtils.show(mContext,"操作失败");
                hideProgress();
            }
        });
    }
    //获取验证码
    private void getVCode(String account) {
        Map<String, String> param = new HashMap<String, String>();
        param.put("userName",account);
        param.put("clientPublicParam", ClientPublicParamUtil.getPublicParams(mContext));
        String url = new HYContent().GET_FORGET_CODE_URL;
        OkGo.<String>post(url).params(param).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                IModel.AjaxResult jr = new IModel.AjaxResult(response.body());
                final int code = jr.getCode();
                if (code>0){

                    ToastUtils.show(mContext,"发送验证码成功！");
                }else{
                    ToastUtils.show(mContext,jr.getMsg());
                }
            }
        });
    }




}
